package org.jeecg.modules.sggg.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.modules.sggg.entity.SgServicePackage;
import org.jeecg.modules.sggg.entity.SgServicePackageItem;
import org.jeecg.modules.sggg.service.ISgServicePackageItemService;
import org.jeecg.modules.sggg.service.ISgServicePackageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Arrays;
import java.util.List;

/**
 * @Description: 服务包表
 * @Author: jeecg-boot
 * @Date: 2022-10-17
 * @Version: V1.0
 */
@Api(tags = "服务包表")
@RestController
@RequestMapping("/sggg/sgServicePackage")
@Slf4j
public class SgServicePackageController extends JeecgController<SgServicePackage, ISgServicePackageService> {
    @Autowired
    private ISgServicePackageService sgServicePackageService;

    @Autowired
    private ISgServicePackageItemService sgServicePackageItemService;

    @ApiOperation(value = "服务包表-分页列表查询", notes = "服务包表-分页列表查询")
    @GetMapping(value = "/list")
    public Result<IPage<SgServicePackage>> queryPageList(SgServicePackage sgServicePackage,
                                                         @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
                                                         @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {
        Page<SgServicePackage> page = new Page<>(pageNo, pageSize);
        IPage<SgServicePackage> pageList = sgServicePackageService.queryList(page, sgServicePackage);
        return Result.OK(pageList);
    }

    /**
     * lx ---- 2022年11月5日 服务包宝海服务项列表
     *
     * @param sgServicePackage 服务包对象
     * @return 列表
     */
    @ApiOperation(value = "服务包表-服务包[服务项]", notes = "服务包表-服务包[服务项]")
    @GetMapping(value = "/packageItemList")
    public Result<List<SgServicePackage>> packageItemList(SgServicePackage sgServicePackage) {

        List<SgServicePackage> pageList = sgServicePackageService.packageItemList(sgServicePackage);
        return Result.OK(pageList);
    }

    @AutoLog(value = "服务包表-添加")
    @ApiOperation(value = "服务包表-添加", notes = "服务包表-添加")
    @PostMapping(value = "/add")
    public Result<String> add(@RequestBody SgServicePackage sgServicePackage) {
        sgServicePackageService.save(sgServicePackage);

        SgServicePackageItem sgServicePackageItem = new SgServicePackageItem();
        String[] splitTid = sgServicePackage.getTid().split(",");
        for (String s : splitTid) {
            sgServicePackageItem.setPid(sgServicePackage.getId());
            sgServicePackageItem.setTid(s);
            sgServicePackageItemService.save(sgServicePackageItem);
        }
        return Result.OK("添加成功！");
    }

    @AutoLog(value = "服务包表-编辑")
    @ApiOperation(value = "服务包表-编辑", notes = "服务包表-编辑")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<String> edit(@RequestBody SgServicePackage sgServicePackage) {
        sgServicePackageService.updateById(sgServicePackage);

        //删除关系表数据
        sgServicePackageItemService.remove(new QueryWrapper<SgServicePackageItem>().eq("pid", sgServicePackage.getId()));

        //重新添加关系表数据
        SgServicePackageItem sgServicePackageItem = new SgServicePackageItem();
        //获取服务项Tid
        String tid = sgServicePackage.getTid();
        String[] splitTid = tid.split(",");

        for (String s : splitTid) {
            sgServicePackageItem.setPid(sgServicePackage.getId());
            sgServicePackageItem.setTid(s);
            sgServicePackageItemService.save(sgServicePackageItem);
        }

        return Result.OK("编辑成功!");
    }

    @AutoLog(value = "服务包表-通过id删除")
    @ApiOperation(value = "服务包表-通过id删除", notes = "服务包表-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<String> delete(@RequestParam(name = "id") String id) {
        sgServicePackageService.removeById(id);
        //删除关系表数据
        sgServicePackageItemService.remove(new QueryWrapper<SgServicePackageItem>().eq("pid", id));
        return Result.OK("删除成功!");
    }

    @AutoLog(value = "服务包表-批量删除")
    @ApiOperation(value = "服务包表-批量删除", notes = "服务包表-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<String> deleteBatch(@RequestParam(name = "ids") String ids) {
        this.sgServicePackageService.removeByIds(Arrays.asList(ids.split(",")));
        return Result.OK("批量删除成功!");
    }

    @ApiOperation(value = "服务包表-通过id查询", notes = "服务包表-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<SgServicePackage> queryById(@RequestParam(name = "id") String id) {
        SgServicePackage sgServicePackage = sgServicePackageService.getById(id);
        if (sgServicePackage == null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(sgServicePackage);
    }

    /**
     * 导出excel
     */
    @RequestMapping(value = "/exportXls")
    public ModelAndView exportXls(HttpServletRequest request, SgServicePackage sgServicePackage) {
        return super.exportXls(request, sgServicePackage, SgServicePackage.class, "服务包表");
    }

    /**
     * 通过excel导入数据
     */
    @RequestMapping(value = "/importExcel", method = RequestMethod.POST)
    public Result<?> importExcel(HttpServletRequest request, HttpServletResponse response) {
        return super.importExcel(request, response, SgServicePackage.class);
    }

}
